Page({
  data: {
    userInfo: {},
    nickName: '',
    phone: '',
    contactInfo: '',
    jkUrl:''
        // jkUrl:'http://172.16.130.18:8080'
    // jkUrl:'https://wxapi.kunszbq.cn'
  },

  onLoad() {
      this.setData({
      jkUrl:this.globalData.jkUrl
    });
    // 检查登录状态
    const app = getApp();
    // if (!app.globalData.userInfo) {
    //   wx.redirectTo({
    //     url: '/pages/login/login'
    //   });
    //   return;
    // }
    // 获取用户信息
    this.setData({
      nickName: app.globalData.userInfo.name || '',
      phone: app.globalData.userInfo.phone || ''
    });
  },

  onPasswordInput(e) {
    this.setData({
      nickName: e.detail.value
    });
  },

  onConfirmPasswordInput(e) {
    this.setData({
      phone: e.detail.value
    });
  },

  onContactInput(e) {
    this.setData({
      contactInfo: e.detail.value
    });
  },

  saveSettings() {
    const { nickName, phone, contactInfo } = this.data;
    
    // 验证密码
    
    // 显示加载提示
    wx.showLoading({
      title: '保存中...'
    });

    const app = getApp();

     var str= {
      userId: app.globalData.userInfo.id,
      userName:app.globalData.userInfo.userName,
      nickName:nickName,
      phonenumber:phone
    };

     wx.request({
      url: this.data.jkUrl+'/system/user/editNew', // 替换为实际的API地址
      method: 'POST',
      data:str,
      success: (res) => {
        wx.hideLoading();
        if (res.data && res.data.code === 200) {
          const data = res.data.data;
          console.log('成功:', data);
          // 调用全局登录方法
           wx.showToast({ title: '设置成功', icon: 'none' });
             const app = getApp();
          const token = 'mock-token-' + Date.now();
          wx.setStorageSync('token', token);
          app.globalData.hasLogin = true;
          app.globalData.token = token;
          app.globalData.userInfo.name = nickName;
          app.globalData.userInfo.phone = phone;
        

           setTimeout(() => {
             wx.navigateBack({
               delta: 1
             });
          }, 1000);
        
          // wx.switchTab({
          //   url: '/pages/home/home'
          // });
        
          
        } else {
          console.error('登录识别:', res.data);
          wx.showToast({ title: '获取数据失败', icon: 'none' });
          // 如果接口调用失败，使用默认数据
          this.setDefaultData();
        }
      },
      fail: (err) => {
        wx.hideLoading();
        console.error('网络请求失败:', err);
        wx.showToast({ title: '网络请求失败', icon: 'none' });
        // 如果网络请求失败，使用默认数据
        this.setDefaultData();
      },
      complete: () => {
        wx.hideLoading();
      }
    });
      
  }
  
});